package managedbean.SaleInquiry;

import entity.SaleInquiry;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SaleInquiryBeanLocal;


@ManagedBean(name="editSaleInquiryManagerBean")
@ViewScoped
public class EditSaleInquiryManagerBean {

    public EditSaleInquiryManagerBean() {
    }
    
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    private SaleInquiry saleInquiryToEdit;
    private String statusMessage;

    public SaleInquiry getSaleInquiryToEdit() {
        return saleInquiryToEdit;
    }
    public void setSaleInquiryToEdit(SaleInquiry saleInquiryToEdit) {
        this.saleInquiryToEdit = saleInquiryToEdit;
    }
    
    public void initView(PhaseEvent event){
        if(saleInquiryToEdit==null){
            saleInquiryToEdit = (SaleInquiry)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleInquiry");
            
        }
    }
    
    public void updateSaleInquiry(ActionEvent event){
        
        try{
         saleInquiryBeanLocal.updateSaleInquiry(saleInquiryToEdit.getId(), saleInquiryToEdit.getLineItems());
        
         statusMessage = "Sale Inquiry Updated Successfully"; 
        
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Sale Inquiry Result: " + statusMessage + " (Sale Inquiry ID is " + saleInquiryToEdit.getId().toString() + ")", "")); 
        }
        catch(Exception ex){
         ex.printStackTrace();   
        }
        
    }    
}
